﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Net;
using System.Net.Mail;
/// <summary>
/// Summary description for GlobalFunctions
/// </summary>
public class GlobalFunctions : System.Web.UI.Page
{
	public GlobalFunctions()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public string getRoleName()
    {
        if (IsLogin() == 0)
            return "NotLogin";
        switch((int)Session["Role"])
        {
            case 1:
                return "Admin";
                break;
            case 2:
                return "Khách hàng thân thiết";
                break;
            default:
                return "Khách hàng đại chúng";
                break;
        }
    }
    public string getCMND()
    {
        if (IsLogin() == 0)
            return "NotLogin";
        return (string)Session["CMND"];
    }
    public int getLoginId()
    {
        if (IsLogin() == 0)
            return -1;
        return (int)Session["LoginID"];
    }
        


    // ham khoi tao session khi nguoi dung dang nhap
    public void initSession(int loginId, int role, string CMND)
    {
        Session["LoginID"] = loginId;
        Session["Role"] = role;
        Session["CMND"] = CMND;
        Session.Timeout = 150;
    }

    //ham kiem tra co dang nhap chua (session co ton tai hay khong)
    public int IsLogin()
    {
        if (Session["LoginID"] == null)
            return 0;
        return 1;
    }

    // ham kiem tra user hien tai co phai dang nhap duoi quyen admin khong
    public int IsAdmin()
    {
        if (Session["LoginID"] == null || (int)Session["Role"] != 1)
            return 0;
        return 1;
    }
    // ham kiem tra user hien tai co phai dang nhap duoi quyen KHTT khong
    public int IsKHTT()
    {
        if (Session["LoginID"] == null || (int)Session["Role"] != 2)
            return 0;
        return 1;
    }
    // ham kiem tra user hien tai co phai dang nhap duoi quyen KHDC khong
    public int IsKHDC()
    {
        if (Session["LoginID"] == null || (int)Session["Role"] != 3)
            return 0;
        return 1;
    }


    //Ham tao hash tu password nguoi dung nhap vao va muoi
    static public string createHash(string passwd, string salt)
    {
        HashAlgorithm myHash = new MD5CryptoServiceProvider();
        string total = passwd + salt;
        byte[] buffer = new byte[total.Length];
        buffer = ASCIIEncoding.ASCII.GetBytes(total);
        byte[] result = myHash.ComputeHash(buffer);
        string pphrase = "";
        for (int i = 0; i < result.Length; i++)
            pphrase += result[i].ToString("X2");
        return pphrase;
    }
    // kiem tra password user nhap vao co dung voi pass trong csdl ko
    static public bool verifyPassword(string userPass, string DB_hash, string DB_salt)
    {
        //Require function: createHash(string passwd, string saltValue)
        //Hash user input password + DB_salt        
        string user_hash = GlobalFunctions.createHash(userPass, DB_salt);
        if (user_hash == DB_hash)
            return true;
        return false;
    }

    static public string SQLInjection_filter(string userInput)
    {
        char[] c = {'@','&','=','+','*','-',',','\'','<','>'};
        userInput = userInput.Trim(c);
        return userInput;
    }
    static public string GetCurrentPageName()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;        
        return sRet;
    }
    static public string getCurrentURL()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
        return sPath;
    }

    //ham kiem tra email nhap vao co hop le khong; 1: hop le; 0: khong hop le
    static public bool IsValidEmail(string email)
    {
        return false;
    }

    static public string MakePassword(int length)
    {
        Random ran = new Random(DateTime.Now.Second);
        char[] password = new char[length];
        for (int i = 0; i < length; i++)
        {
            int[] n = { ran.Next(48, 57), ran.Next(65, 90), ran.Next(97, 122) };
            //int[] n = {ran.Next(33, 57), ran.Next(65, 90), ran.Next(97, 122)};
            int picker = ran.Next(0, 3);
            if (picker == 3)//if i make the maxvalue 2 it "never" appears... dunno whats going on there
                picker = 2;
            password[i] = (char)n[picker];
        }
        return new string(password);
    }
    static  public void sendEmail(string To, string subject, string body)
    {
        try
        {
            MailMessage msg = new MailMessage();
            msg.IsBodyHtml = true;

            msg.Body = (body);
            msg.BodyEncoding = System.Text.UTF8Encoding.UTF8;
            string mailAdd = ConfigurationSettings.AppSettings["EMAIL_ADDRESS"].ToString().Trim();
            string pass = ConfigurationSettings.AppSettings["EMAIL_PASSWORD"].ToString().Trim();
            msg.From = new MailAddress(mailAdd);
            msg.To.Add(new MailAddress(To));
            msg.Subject = subject;

            //SmtpClient client = new SmtpClient("smtp.mail.yahoo.com.vn", 25);
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.Credentials = new NetworkCredential(mailAdd, pass);
            client.EnableSsl = true;
            client.Send(msg);
        }
        catch (Exception ex)
        {
            
        }
    }

}
